<?php

namespace app\xwam\controller;

/**
 * 验证码控制器
 * 
 * @version 1.0 2014-6-13 by sutroon
 * @version 1.0.0 2015-4-15 SoChishun(14507247@qq.com) Added.
 * @version 2.0 2017-7-6
 * @version 2.1 2018-1-23 重构
 */
class CaptchaController {

    /**
     * 生成验证码
     * @param string $id
     * @version 1.0 2014-6-13> sutroon Added.
     * @version 2.0 2015-4-15 SoChishun Refactoring function..
     * @version 2.1 2015-4-17 SoChishun Refactoring params.
     */
    public function index($id = 'captcha', $len = 4, $height = 22, $fontsize = 12, $width = 0) {
        
        $len = intval($len);
        if ($len < 1 || $len > 6) {
            $len = 4;
        }
        $height = intval($height);
        if ($height < 10 || $height > 999) {
            $height = 22;
        }
        if (!is_numeric($width)) {
            $width = 0;
        }
        $fontsize = intval($fontsize);
        if ($fontsize < 1 || $fontsize > 999) {
            $fontsize = 12;
        }
        $idlen = strlen($id);
        if ($idlen < 1 || $idlen > 32) {
            $id = 'captcha';
        }
        $config = array(
            'length' => $len,
            'imageH' => $height,
            'fontSize' => $fontsize,
            'useCurve' => false,
            'useNoise' => false,
            'fontttf' => '1.ttf',
        );
        if ($width > 0) {
            $config['imageW'] = $width;
        }
        return $this->entry($config);
    }

    /**
     * 生成验证码图片
     * @param array $config
     * @param string $id
     * @version 1.0 2015-4-17 SoChishun Added.
     */
    public function entry($config = array(), $id = 'captcha') {
        $options = array(
            'expire'=>1800, // 验证码过期时间（s）
            'length' => 4, // 验证码位数
            'imageH' => 22, // 验证码图片高度，设置为0为自动计算
            'fontSize' => 12, // 验证码字体大小(px)
            'useCurve' => false, // 是否画混淆曲线
            'useNoise' => false, // 是否添加杂点
            'fontttf' => '1.ttf', // 验证码字体，不设置随机获取
            'useImgBg'=>false, // 使用背景图片
            'codeSet' => '012345679',
        );
        $config = array_merge($options, $config);
        $captcha = new \think\captcha\Captcha($config);
        return $captcha->entry($id);
    }

    /**
     * 检验验证码
     * @param string $code
     * @param string $id
     * @param boolean $reset 是否重置验证码
     * @return boolean
     * @version 1.0 2014-6-13> sutroon Added.
     * @version 2.0 2015-4-15 SoChishun Refactoring function.
     */
    public function check_verify($code, $id = 'captcha', $reset = true) {
        $captcha = new \think\captcha\Captcha(array('reset' => $reset));
        return $captcha->check($code, $id);
    }
    
    public static function checkCaptcha($code, $id = 'captcha', $reset = true) {
        $captcha = new \think\captcha\Captcha(array('reset' => $reset));
        return $captcha->check($code, $id);
    }

    /**
     * 动态确认验证码是否正确
     * @param $v
     * @version 1.0 2014-9-27 by sutroon
     * @example
     * $('input[name="captcha"]').keyup(function(){
      var val=$(this).val();
      if(val.length>3){
      var url='{:U("Captcha/confirm","v=value")}';
      $.get(url.replace('value',val),function(data){
      $('.captcha-field label i').html(data?'[正确]':'[错误]');
      });
      }
      })
     */
    public function confirm($code, $id = 'captcha') {
        $captcha = new \think\captcha\Captcha(array('reset' => false));
        return $captcha->check($code, $id);
    }

}